<?php
namespace app\common\service\MessageService;
use app\Normal;
use app\model\BbcModel\Message;

class MessageService
{
    /**
     * 获取消息列表分页
     * @param  $param
     * @return array
     */
    public function getListByPage( $param):array {
        $page=(isset($param['page'])&&$param['page']>0)?$param['page']:1;
        $limit=(isset($param['limit'])&&$param['limit']>0)?$param['limit']:20;
        $where=[
            ['df','=',0]
        ];
        if(isset($param['status']) && $param['status'] !==''){
            $where[] = ['status', '=', $param['status']];
        }
        if (!empty($param['cd_start'])) {
            $where[] = ['cd', '>=', strtotime($param['cd_start'])];
        }
        if (!empty($param['cd_end'])) {
            $where[] = ['cd', '<=', strtotime($param['cd_end'])];
        }
        $data= Message::instance()->listPages($where, $limit, $page);
        $count= Message::instance()->countNum($where);
        foreach ($data as &$item) {
            $item['cd']=$item['cd']?date('Y-m-d H:i:s',$item['cd']):'';
            $item['md']=$item['md']?date('Y-m-d H:i:s',$item['md']):'';
            $item['status_name']= Message::instance()->status[$item['status']];
        }
        return Normal::returnCode(Normal::SUC,"获取成功",['list'=>$data,'total'=>$count]);
    }
    
    /**
     * 新增消息
     * @param array $param
     * @return array
     */
    public function addMessage(array $param):array {
        $title = trim($param['title']) ?? '';
        $description = trim($param['description']) ?? '';
        $content = $param['content'] ?? '';
        if (empty($title)) {
            return Normal::returnCode(Normal::FAIL,"标题不能为空");
        }
        if (empty($param['type'])) {
            return Normal::returnCode(Normal::FAIL,"类型不能为空");
        }
        if (empty($description)) {
            return Normal::returnCode(Normal::FAIL,"描述不能为空");
        }
        if (empty($content)) {
            return Normal::returnCode(Normal::FAIL,"内容不能为空");
        }
        $add_data = [
            'type'=>$param['type'],
            'title' => $title,
            'description' => $description,
            'content' => $content,
            'status' => 0,
            'cd' => time(),
            'cid' => 0,
            'create_id'=> 0
        ];
        $res = Message::instance()->add($add_data);
        if($res){
            return Normal::returnCode(Normal::SUC,"新增成功");
        }else{
            return Normal::returnCode(Normal::FAIL,"新增失败");
        }
    }
    
    /**
     * 修改消息
     * @param array $param
     * @return array
     */
    public function editMessage(array $param):array {
        if (empty($param['id'])) {
            return Normal::returnCode(Normal::FAIL,"id必传");
        }
        $message= Message::instance()->info(['id'=>$param['id']]);
        if(empty($message)){
            return Normal::returnCode(Normal::FAIL,"未找到相关信息");
        }
        $title = trim($param['title']) ?? '';
        $description = trim($param['description']) ?? '';
        $content = $param['content'] ?? '';
        if (empty($title)) {
            return Normal::returnCode(Normal::FAIL,"标题不能为空");
        }
        if (empty($param['type'])) {
            return Normal::returnCode(Normal::FAIL,"类型不能为空");
        }
        if (empty($description)) {
            return Normal::returnCode(Normal::FAIL,"描述不能为空");
        }
        if (empty($content)) {
            return Normal::returnCode(Normal::FAIL,"内容不能为空");
        }
        $edit_data = [
            'type'=>$param['type'],
            'title' => $title,
            'description' => $description,
            'content' => $content,
            'md' => time(),
        ];
        $res = Message::instance()->edit(['id'=>$param['id']], $edit_data);
        if($res){
            return Normal::returnCode(Normal::SUC,"修改成功");
        }else{
            return Normal::returnCode(Normal::FAIL,"修改失败");
        }
    }
    
    /**
     * 删除消息
     * @param array $param
     * @return array
     */
    public function delMessage(array $param):array {
        if (empty($param['id'])) {
            return Normal::returnCode(Normal::FAIL,"id必传");
        }
        $message= Message::instance()->info(['id'=>$param['id']]);
        if(empty($message)){
            return Normal::returnCode(Normal::FAIL,"未找到相关信息");
        }
        if($message['status']==1){
            return Normal::returnCode(Normal::FAIL,"已发送消息不能删除");
        }
        $res = Message::instance()->edit(['id'=>$param['id']], ['df'=>1,'md'=>time()]);
        if($res){
            return Normal::returnCode(Normal::SUC,"删除成功");
        }else{
            return Normal::returnCode(Normal::FAIL,"删除失败");
        }
    }
    
    /**
     * 发送消息
     * @param array $param
     * @return array
     */
    public function sendMessage(array $param):array {
        if (empty($param['id'])) {
            return Normal::returnCode(Normal::FAIL,"id必传");
        }
        $message= Message::instance()->info(['id'=>$param['id']]);
        if(empty($message)){
            return Normal::returnCode(Normal::FAIL,"未找到相关信息");
        }
        if($message['status']==1){
            return Normal::returnCode(Normal::FAIL,"消息已发送");
        }
        $res = Message::instance()->edit(['id'=>$param['id']], ['status'=>1,'md'=>time()]);
        if($res){
            return Normal::returnCode(Normal::SUC,"发送成功");
        }else{
            return Normal::returnCode(Normal::FAIL,"发送失败");
        }
    }
}
